/*******************************************************************************
 * Copyright (c) 2008-2009 zhang yuexiang.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.xfeep.asura.bootstrap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xfeep.asura.core.ComponentDefinition;
import org.xfeep.asura.core.ComponentManager;

public class MemoryComponentLoader implements ComponentLoader {

	static String[] list = {
		"org.xfeep.asura.core.config.ConfigAdminService",
		"org.xfeep.asura.core.config.ConfigServiceImp",
//		"org.xfeep.asura.conf.jaxb.JAXBConfServiceImp",
		"org.xfeep.asura.core.ComponentFactoryServiceImp",
//		"org.xfeep.asura.core.console.CommandLineService",
		 org.xfeep.asura.core.logger.TinyLogService.class.getName(),
		"org.xfeep.asura.core.SimpleRegisterService",
//		"org.xfeep.asura.remote.bindings.HessianBinding",
//		"org.xfeep.asura.remote.http.GrizzlyHttpService",
//		"org.xfeep.asura.remote.http.HttpTransport",
//		"org.xfeep.asura.remote.http.SmartCookieHandler",
//		"org.xfeep.asura.remote.RemoteServiceLocator",
//		"org.xfeep.asura.remote.SynExchangeHandler"
	};
	
	Class<?>[] coreComponentList;
	String[] coreComponentStringList;
	
	public MemoryComponentLoader() {
		this(getCoreComponentList());
	}
	
	public MemoryComponentLoader(String... coreComponentList) {
		this.coreComponentStringList = coreComponentList;
	}
	
	public MemoryComponentLoader(Class<?>... coreComponentList) {
		this.coreComponentList = coreComponentList;
	}

	public static String[] getCoreComponentList() {
		return list;
	}
	
	public List<ComponentDefinition> getStaticLoadComponents(
			ClassLoader classLoader) throws IOException {
		if (coreComponentStringList != null){
			coreComponentList = new Class<?>[coreComponentStringList.length];
			for (int i = 0; i < coreComponentList.length; i++){
				try {
					coreComponentList[i] = classLoader.loadClass(coreComponentStringList[i]);
				} catch (ClassNotFoundException e) {
					throw new IOException("can not load component " + coreComponentStringList[i], e);
				}
			}
		}
		List<ComponentDefinition> rt = new ArrayList<ComponentDefinition>();
		for (Class<?> c : coreComponentList){
			rt.add( ComponentDefinition.create(c) );
		}
		return rt;
		
	}

	public void initialize(ComponentManager componentManager) {

	}

	public void setupComponentDynamicLoadListener(
			ComponentDynamicLoadListener listener) {

	}

}
